/**
 * @description orderModel Service
 */

 const { orderModel, seatModel, trainModel } = require('../db/index')
 const Op = require('sequelize').Op
 
 async function addData(params) {
  //  获取所有该车次该时间断的座位信息
  const seats = await seatModel.findAll({
    where: {
      date: params.ticketTime,
      trainName: params.trainName
    }
  })
  let passangers = JSON.parse(params.passangers)
  passangers.forEach(async (v, i) => {
    if (params.seatInfo === '无座') {
      passangers[i].seats = '01车厢'
      // 新增seat
      await seatModel.create({
        no: '无座',
        date: params.ticketTime,
        trainName: params.trainName
      })
    } else {
      passangers[i].seats = '01车厢' + (seats.length + i + 1) + '号';
      console.log({
        no: '1',
        date: params.ticketTime,
        trainName: params.trainName
      })
      await seatModel.create({
        no: '1',
        date: params.ticketTime,
        trainName: params.trainName
      })
    }
  })
   const result = await orderModel.create({
    ...params,
    passangers: JSON.stringify(passangers)
   })
   const data = result.dataValues
   return data
 }
 
 /**
  * 
  * @param {string} username 用户名 
  */
 async function getUserInfo(username) {
   // 查询条件
   const whereOption = {
    username
   }
   const result = await orderModel.findOne({
     where: whereOption
   })
   if (result === null) {
     return result
   }
   return result.dataValues
 }
 
 /**
  * 修改用户信息
  * @param {*} param0 要修改的用户信息
  * @param {*} param1 查询条件
  */
 async function updateData(
   params,
   id
 ) {
 
   const whereOption = {}
   if (id) {
     whereOption.id = id
   }
  if (!params.passangers) {
    const result = await orderModel.update(params, {
       where: whereOption
     })
     return result[0] > 0
  }
  const seats = await seatModel.findAll({
    where: {
      date: params.ticketTime,
      trainName: params.trainName
    }
  })
  let passangers = JSON.parse(params.passangers)
  passangers.forEach(async (v, i) => {
    if (params.seatInfo === '无座') {
      passangers[i].seats = '01车厢'
      // 新增seat
      await seatModel.create({
        no: '无座',
        date: params.ticketTime,
        trainName: params.trainName
      })
    } else {
      passangers[i].seats = '01车厢' + (seats.length + i + 1) + '号';
      console.log({
        no: '1',
        date: params.ticketTime,
        trainName: params.trainName
      })
      await seatModel.create({
        no: '1',
        date: params.ticketTime,
        trainName: params.trainName
      })
    }
  })
 
   // 执行修改
   const result = await orderModel.update({
    ...params,
    passangers: JSON.stringify(passangers)
   }, {
     where: whereOption
   })
   return result[0] > 0
 }
 
 /**
  * 删除用户
  * @param {string} userid 用户id 
  */
 async function deleteData(id) {
   const result = await orderModel.destroy({
     where: {
       id
     }
   })
   return result > 0
 }
 
 /**
  * 分页查询所有用户
  * @param {number} page 当前页码
  * @param {number} pageSize 每页条数
  */
 async function queryAll(params) {
  const {page = 1, pageSize = 10, userId} = params
   const whereOption = {}
   if (userId) {
     whereOption.userId = userId
   }
  //  if (userid) {
  //    whereOption.userid = {
  //      [Op.like]: `%${userid}%`
  //    }
  //  }
 
   if (page < 1) {
     page = 1
   }
   const result = await orderModel.findAll({
     limit: pageSize,
     offset: (page - 1) * pageSize,
     where: whereOption
   })
   const total = await orderModel.findAll()
   return {
     page,
     pageSize,
     total: total.length || 0,
     data: result || []
   }
 }
 
 module.exports = {
  addData,
   getUserInfo,
   updateData,
   deleteData,
   queryAll,
 }
 